class Solution:
    def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
        n = len(profits)
        arr = [[capital[i], profits[i]] for i in range(n)]
        arr.sort()

        pq = []
        curr = 0
        for i in range(k):
            while curr < n and arr[curr][0] <= w:
                heapq.heappush(pq, -arr[curr][1])
                curr += 1

            if pq:
                w -= heapq.heappop(pq)
            else:
                break

        return w